home *** CD-ROM | disk | FTP | other *** search
/ Super PC 33 / Super PC 33 (Shareware).iso / spc / sonido / timidity / source / instrume.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-03  |  17.0 KB  |  672 lines

  1. /*
  2.  
  3.     TiMidity -- Experimental MIDI to WAVE converter
  4.     Copyright (C) 1995 Tuukka Toivonen <titoivon@snakemail.hut.fi>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.    instruments.c 
  21.    
  22.    Code to load and unload GUS-compatible instrument patches.
  23.  
  24. */
  25.  
  26. #include <stdio.h>
  27. #ifdef __WIN32__
  28. #include <string.h>
  29. #else
  30. #include <strings.h>
  31. #endif
  32.  
  33. #if defined (__FreeBSD__) || defined (__WIN32__)
  34. #include <stdlib.h>
  35. #else
  36. #include <malloc.h>
  37. #endif
  38.  
  39. #include "config.h"
  40. #include "common.h"
  41. #ifdef __WIN32__
  42. #include "instrume.h"
  43. #include "filterin.h"
  44. #else
  45. #include "instruments.h"
  46. #include "filtering.h"
  47. #endif
  48. #include "playmidi.h"
  49. #include "output.h"
  50. #include "controls.h"
  51. #include "resample.h"
  52. #include "tables.h"
  53.  
  54. /* Some functions get aggravated if not even the standard banks are
  55.    available. */
  56. static ToneBank standard_tonebank, standard_drumset;
  57. ToneBank 
  58.   *tonebank[128]={&standard_tonebank},
  59.   *drumset[128]={&standard_drumset};
  60.  
  61. /* This is a special instrument, used for all melodic programs */
  62. Instrument *default_instrument=0;
  63.  
  64. /* This is only used for tracks that don't specify a program */
  65. int default_program=DEFAULT_PROGRAM;
  66.  
  67. int antialiasing_allowed=0;
  68. #ifdef FAST_DECAY
  69. int fast_decay=1;
  70. #else
  71. int fast_decay=0;
  72. #endif
  73.  
  74. static void free_instrument(Instrument *ip)
  75. {
  76.   Sample *sp;
  77.   int i;
  78.   if (!ip) return;
  79.   for (i=0; i<ip->samples; i++)
  80.     {
  81.       sp=&(ip->sample[i]);
  82.       free(sp->data);
  83.     }
  84.   free(ip->sample);
  85.   free(ip);
  86. }
  87.  
  88. static void free_bank(int dr, int b)
  89. {
  90.   int i;
  91.   ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
  92.   for (i=0; i<128; i++)
  93.     if (bank->tone[i].instrument)
  94.       {
  95.     /* Not that this could ever happen, of course */
  96.     if (bank->tone[i].instrument != MAGIC_LOAD_INSTRUMENT)
  97.       free_instrument(bank->tone[i].instrument);
  98.     bank->tone[i].instrument=0;
  99.       }
  100. }
  101.  
  102. static int32 convert_envelope_rate(uint8 rate)
  103. {
  104.   int32 r;
  105.  
  106.   r=3-((rate>>6) & 0x3);
  107.   r*=3;
  108.   r = (int32)(rate & 0x3f) << r; /* 6.9 fixed point */
  109.  
  110.   /* 15.15 fixed point. */
  111.   return (((r * 44100) / play_mode->rate) * control_ratio) 
  112.     << ((fast_decay) ? 10 : 9);
  113. }
  114.  
  115. static int32 convert_envelope_offset(uint8 offset)
  116. {
  117.   /* This is not too good... Can anyone tell me what these values mean?
  118.      Are they GUS-style "exponential" volumes? And what does that mean? */
  119.  
  120.   /* 15.15 fixed point */
  121.   return offset << (7+15);
  122. }
  123.  
  124. static int32 convert_tremolo_sweep(uint8 sweep)
  125. {
  126.   if (!sweep)
  127.     return 0;
  128.  
  129.   return
  130.     ((control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
  131.       (play_mode->rate * sweep);
  132. }
  133.  
  134. static int32 convert_vibrato_sweep(uint8 sweep, int32 vib_control_ratio)
  135. {
  136.   if (!sweep)
  137.     return 0;
  138.  
  139.   return
  140.     (int32) (FSCALE((double) (vib_control_ratio) * SWEEP_TUNING, SWEEP_SHIFT)
  141.          / (double)(play_mode->rate * sweep));
  142.  
  143.   /* this was overflowing with seashore.pat
  144.  
  145.       ((vib_control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
  146.       (play_mode->rate * sweep); */
  147. }
  148.  
  149. static int32 convert_tremolo_rate(uint8 rate)
  150. {
  151.   return
  152.     ((SINE_CYCLE_LENGTH * control_ratio * rate) << RATE_SHIFT) /
  153.       (TREMOLO_RATE_TUNING * play_mode->rate);
  154. }
  155.  
  156. static int32 convert_vibrato_rate(uint8 rate)
  157. {
  158.   /* Return a suitable vibrato_control_ratio value */
  159.   return
  160.     (VIBRATO_RATE_TUNING * play_mode->rate) / 
  161.       (rate * 2 * VIBRATO_SAMPLE_INCREMENTS);
  162. }
  163.  
  164. static void reverse_data(int16 *sp, int32 ls, int32 le)
  165. {
  166.   int16 s, *ep=sp+le;
  167.   sp+=ls;
  168.   le-=ls;
  169.   le/=2;
  170.   while (le--)
  171.     {
  172.       s=*sp;
  173.       *sp++=*ep;
  174.       *ep--=s;
  175.     }
  176. }
  177.  
  178. /* 
  179.    If panning or note_to_use != -1, it will be used for all samples,
  180.    instead of the sample-specific values in the instrument file. 
  181.  
  182.    For note_to_use, any value <0 or >127 will be forced to 0.
  183.  
  184.    For other parameters, 1 means yes, 0 means no, other values are
  185.    undefined.
  186.  
  187.    TODO: do reverse loops right */
  188. static Instrument *load_instrument(char *name, int percussion,
  189.                    int panning, int amp, int note_to_use,
  190.                    int strip_loop, int strip_envelope,
  191.                    int strip_tail)
  192. {
  193.   Instrument *ip;
  194.   Sample *sp;
  195.   FILE *fp;
  196.   uint8 tmp[1024];
  197.   int i,j,noluck=0;
  198. #ifdef PATCH_EXT_LIST
  199.   static char *patch_ext[] = PATCH_EXT_LIST;
  200. #endif
  201.  
  202.   if (!name) return 0;
  203.   
  204.   /* Open patch file */
  205.   if (!(fp=open_file(name, 1, OF_NORMAL)))
  206.     {
  207.       noluck=1;
  208. #ifdef PATCH_EXT_LIST
  209.       /* Try with various extensions */
  210.       for (i=0; patch_ext[i]; i++)
  211.     {
  212.       if (strlen(name)+strlen(patch_ext[i])<1024)
  213.         {
  214.           strcpy(tmp, name);
  215.           strcat(tmp, patch_ext[i]);
  216.           if ((fp=open_file(tmp, 1, OF_NORMAL)))
  217.         {
  218.           noluck=0;
  219.           break;
  220.         }
  221.         }
  222.     }
  223. #endif
  224.     }
  225.   
  226.   if (noluck)
  227.     {
  228.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
  229.         "Instrument `%s' can't be found.", name);
  230.       return 0;
  231.     }
  232.       
  233.   ctl->cmsg(CMSG_INFO, VERB_NOISY, "Loading instrument %s", current_filename);
  234.   
  235.   /* Read some headers and do cursory sanity checks. There are loads
  236.      of magic offsets. This could be rewritten... */
  237.  
  238.   if ((239 != fread(tmp, 1, 239, fp)) ||
  239.       (memcmp(tmp, "GF1PATCH110\0ID#000002", 22) &&
  240.        memcmp(tmp, "GF1PATCH100\0ID#000002", 22))) /* don't know what the
  241.                               differences are */
  242.     {
  243.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: not an instrument", name);
  244.       return 0;
  245.     }
  246.   
  247.   if (tmp[82] != 1 && tmp[82] != 0) /* instruments. To some patch makers, 
  248.                        0 means 1 */
  249.     {
  250.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
  251.        "Can't handle patches with %d instruments", tmp[82]);
  252.       return 0;
  253.     }
  254.  
  255.   if (tmp[151] != 1 && tmp[151] != 0) /* layers. What's a layer? */
  256.     {
  257.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
  258.        "Can't handle instruments with %d layers", tmp[151]);
  259.       return 0;
  260.     }
  261.   
  262.   ip=safe_malloc(sizeof(Instrument));
  263.   ip->samples = tmp[198];
  264.   ip->sample = safe_malloc(sizeof(Sample) * ip->samples);
  265.   for (i=0; i<ip->samples; i++)
  266.     {
  267.  
  268.       uint8 fractions;
  269.       int32 tmplong;
  270.       uint16 tmpshort;
  271.       uint8 tmpchar;
  272.  
  273. #define READ_CHAR(thing) \
  274.       if (1 != fread(&tmpchar, 1, 1, fp)) goto fail; \
  275.       thing = tmpchar;
  276. #define READ_SHORT(thing) \
  277.       if (1 != fread(&tmpshort, 2, 1, fp)) goto fail; \
  278.       thing = LE_SHORT(tmpshort);
  279. #define READ_LONG(thing) \
  280.       if (1 != fread(&tmplong, 4, 1, fp)) goto fail; \
  281.       thing = LE_LONG(tmplong);
  282.  
  283.       skip(fp, 7); /* Skip the wave name */
  284.  
  285.       if (1 != fread(&fractions, 1, 1, fp))
  286.     {
  287.     fail:
  288.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Error reading sample %d", i);
  289.       for (j=0; j<i; j++)
  290.         free(ip->sample[j].data);
  291.       free(ip->sample);
  292.       free(ip);
  293.       return 0;
  294.     }
  295.  
  296.       sp=&(ip->sample[i]);
  297.       
  298.       READ_LONG(sp->data_length);
  299.       READ_LONG(sp->loop_start);
  300.       READ_LONG(sp->loop_end);
  301.       READ_SHORT(sp->sample_rate);
  302.       READ_LONG(sp->low_freq);
  303.       READ_LONG(sp->high_freq);
  304.       READ_LONG(sp->root_freq);
  305.       skip(fp, 2); /* Why have a "root frequency" and then "tuning"?? */
  306.       
  307.       READ_CHAR(tmp[0]);
  308.  
  309.       if (panning==-1)
  310.     sp->panning = (tmp[0] * 8 + 4) & 0x7f;
  311.       else
  312.     sp->panning=(uint8)(panning & 0x7F);
  313.  
  314.       /* envelope, tremolo, and vibrato */
  315.       if (18 != fread(tmp, 1, 18, fp)) goto fail; 
  316.  
  317.       if (!tmp[13] || !tmp[14])
  318.     {
  319.       sp->tremolo_sweep_increment=
  320.         sp->tremolo_phase_increment=sp->tremolo_depth=0;
  321.       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no tremolo");
  322.     }
  323.       else
  324.     {
  325.       sp->tremolo_sweep_increment=convert_tremolo_sweep(tmp[12]);
  326.       sp->tremolo_phase_increment=convert_tremolo_rate(tmp[13]);
  327.       sp->tremolo_depth=tmp[14];
  328.       ctl->cmsg(CMSG_INFO, VERB_DEBUG,
  329.            " * tremolo: sweep %d, phase %d, depth %d",
  330.            sp->tremolo_sweep_increment, sp->tremolo_phase_increment,
  331.            sp->tremolo_depth);
  332.     }
  333.  
  334.       if (!tmp[16] || !tmp[17])
  335.     {
  336.       sp->vibrato_sweep_increment=
  337.         sp->vibrato_control_ratio=sp->vibrato_depth=0;
  338.       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no vibrato");
  339.     }
  340.       else
  341.     {
  342.       sp->vibrato_control_ratio=convert_vibrato_rate(tmp[16]);
  343.       sp->vibrato_sweep_increment=
  344.         convert_vibrato_sweep(tmp[15], sp->vibrato_control_ratio);
  345.       sp->vibrato_depth=tmp[17];
  346.       ctl->cmsg(CMSG_INFO, VERB_DEBUG,
  347.            " * vibrato: sweep %d, ctl %d, depth %d",
  348.            sp->vibrato_sweep_increment, sp->vibrato_control_ratio,
  349.            sp->vibrato_depth);
  350.  
  351.     }
  352.  
  353.       READ_CHAR(sp->modes);
  354.  
  355.       skip(fp, 40); /* skip the useless scale frequency, scale factor
  356.                (what's it mean?), and reserved space */
  357.  
  358.       /* Mark this as a fixed-pitch instrument if such a deed is desired. */
  359.       if (note_to_use!=-1)
  360.     sp->note_to_use=(uint8)(note_to_use);
  361.       else
  362.     sp->note_to_use=0;
  363.       
  364.       /* seashore.pat in the Midia patch set has no Sustain. I don't
  365.          understand why, and fixing it by adding the Sustain flag to
  366.          all looped patches probably breaks something else. We do it
  367.          anyway. */
  368.      
  369.       if (sp->modes & MODES_LOOPING) 
  370.     sp->modes |= MODES_SUSTAIN;
  371.  
  372.       /* Strip any loops and envelopes we're permitted to */
  373.       if ((strip_loop==1) && 
  374.       (sp->modes & (MODES_SUSTAIN | MODES_LOOPING | 
  375.             MODES_PINGPONG | MODES_REVERSE)))
  376.     {
  377.       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Removing loop and/or sustain");
  378.       sp->modes &=~(MODES_SUSTAIN | MODES_LOOPING | 
  379.             MODES_PINGPONG | MODES_REVERSE);
  380.     }
  381.  
  382.       if (strip_envelope==1)
  383.     {
  384.       if (sp->modes & MODES_ENVELOPE)
  385.         ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Removing envelope");
  386.       sp->modes &= ~MODES_ENVELOPE;
  387.     }
  388.       else if (strip_envelope != 0)
  389.     {
  390.       /* Have to make a guess. */
  391.       if (!(sp->modes & (MODES_LOOPING | MODES_PINGPONG | MODES_REVERSE)))
  392.         {
  393.           /* No loop? Then what's there to sustain? No envelope needed
  394.          either... */
  395.           sp->modes &= ~(MODES_SUSTAIN|MODES_ENVELOPE);
  396.           ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
  397.             " - No loop, removing sustain and envelope");
  398.         }
  399.       else if (!memcmp(tmp, "??????", 6) || tmp[11] >= 100) 
  400.         {
  401.           /* Envelope rates all maxed out? Envelope end at a high "offset"?
  402.          That's a weird envelope. Take it out. */
  403.           sp->modes &= ~MODES_ENVELOPE;
  404.           ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
  405.             " - Weirdness, removing envelope");
  406.         }
  407.       else if (!(sp->modes & MODES_SUSTAIN))
  408.         {
  409.           /* No sustain? Then no envelope.  I don't know if this is
  410.          justified, but patches without sustain usually don't need the
  411.          envelope either... at least the Gravis ones. They're mostly
  412.          drums.  I think. */
  413.           sp->modes &= ~MODES_ENVELOPE;
  414.           ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
  415.             " - No sustain, removing envelope");
  416.         }
  417.     }
  418.  
  419.       for (j=0; j<6; j++)
  420.     {
  421.       sp->envelope_rate[j]=
  422.         convert_envelope_rate(tmp[j]);
  423.       sp->envelope_offset[j]= 
  424.         convert_envelope_offset(tmp[6+j]);
  425.     }
  426.  
  427.       /* Then read the sample data */
  428.       sp->data = safe_malloc(sp->data_length);
  429.       if (1 != fread(sp->data, sp->data_length, 1, fp))
  430.     goto fail;
  431.       
  432.       if (!(sp->modes & MODES_16BIT)) /* convert to 16-bit data */
  433.     {
  434.       int32 i=sp->data_length;
  435.       uint8 *cp=(uint8 *)(sp->data);
  436.       uint16 *tmp,*new;
  437.       tmp=new=safe_malloc(sp->data_length*2);
  438.       while (i--)
  439.         *tmp++ = (uint16)(*cp++) << 8;
  440.       cp=(uint8 *)(sp->data);
  441.       sp->data = (sample_t *)new;
  442.       free(cp);
  443.       sp->data_length *= 2;
  444.       sp->loop_start *= 2;
  445.       sp->loop_end *= 2;
  446.     }
  447. #ifndef LITTLE_ENDIAN
  448.       else
  449.     /* convert to machine byte order */
  450.     {
  451.       int32 i=sp->data_length/2;
  452.       int16 *tmp=(int16 *)sp->data,s;
  453.       while (i--)
  454.         { 
  455.           s=LE_SHORT(*tmp);
  456.           *tmp++=s;
  457.         }
  458.     }
  459. #endif
  460.       
  461.       if (sp->modes & MODES_UNSIGNED) /* convert to signed data */
  462.     {
  463.       int32 i=sp->data_length/2;
  464.       int16 *tmp=(int16 *)sp->data;
  465.       while (i--)
  466.         *tmp++ ^= 0x8000;
  467.     }
  468.  
  469.       /* Reverse reverse loops and pass them off as normal loops */
  470.       if (sp->modes & MODES_REVERSE)
  471.     {
  472.       int32 t;
  473.       /* The GUS apparently plays reverse loops by reversing the
  474.          whole sample. We do the same because the GUS does not SUCK. */
  475.  
  476.       ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Reverse loop in %s", name);
  477.       reverse_data((int16 *)sp->data, 0, sp->data_length/2);
  478.  
  479.       t=sp->loop_start;
  480.       sp->loop_start=sp->data_length - sp->loop_end;
  481.       sp->loop_end=sp->data_length - t;
  482.  
  483.       sp->modes &= ~MODES_REVERSE;
  484.       sp->modes |= MODES_LOOPING; /* just in case */
  485.     }
  486.  
  487.       /* If necessary do some anti-aliasing filtering -- this doesn't
  488.      work well with looping yet, so we skip it for looped samples. */
  489.       if (antialiasing_allowed && !(sp->modes & MODES_LOOPING))
  490.     antialiasing(sp,play_mode->rate);
  491.  
  492. #ifdef ADJUST_SAMPLE_VOLUMES
  493.       if (amp!=-1)
  494.     sp->volume=(double)(amp) / 100.0;
  495.       else
  496.     {
  497.       /* Try to determine a volume scaling factor for the sample.
  498.          This is a very crude adjustment, but things sound more
  499.          balanced with it. Still, this should be a runtime option. */
  500.       int32 i=sp->data_length/2;
  501.       int16 maxamp=0,a;
  502.       int16 *tmp=(int16 *)sp->data;
  503.       while (i--)
  504.         {
  505.           a=*tmp++;
  506.           if (a<0) a=-a;
  507.           if (a>maxamp)
  508.         maxamp=a;
  509.         }
  510.       sp->volume=32768.0 / (double)(maxamp);
  511.       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * volume comp: %f", sp->volume);
  512.     }
  513. #else
  514.       if (amp!=-1)
  515.     sp->volume=(double)(amp) / 100.0;
  516.       else
  517.     sp->volume=1.0;
  518. #endif
  519.  
  520.       sp->data_length /= 2; /* These are in bytes. Convert into samples. */
  521.       sp->loop_start /= 2;
  522.       sp->loop_end /= 2;
  523.  
  524.       /* Then fractional samples */
  525.       sp->data_length <<= FRACTION_BITS;
  526.       sp->loop_start <<= FRACTION_BITS;
  527.       sp->loop_end <<= FRACTION_BITS;
  528.  
  529.       /* Adjust for fractional loop points. This is a guess. Does anyone
  530.      know what "fractions" really stands for? */
  531.       sp->loop_start |=
  532.     (fractions & 0x0F) << (FRACTION_BITS-4);
  533.       sp->loop_end |=
  534.     ((fractions>>4) & 0x0F) << (FRACTION_BITS-4);
  535.  
  536.       /* If this instrument will always be played on the same note,
  537.      and it's not looped, we can resample it now. */
  538.       if (sp->note_to_use && !(sp->modes & MODES_LOOPING))
  539.     pre_resample(sp);
  540.  
  541. #ifdef LOOKUP_HACK
  542.       /* Squash the 16-bit data into 8 bits. */
  543.       {
  544.     uint8 *gulp,*ulp;
  545.     int16 *swp;
  546.     int l=sp->data_length >> FRACTION_BITS;
  547.     gulp=ulp=safe_malloc(l+1);
  548.     swp=(int16 *)sp->data;
  549.     while(l--)
  550.       *ulp++ = (*swp++ >> 8) & 0xFF;
  551.     free(sp->data);
  552.     sp->data=(sample_t *)gulp;
  553.       }
  554. #endif
  555.       
  556.       if (strip_tail==1)
  557.     {
  558.       /* Let's not really, just say we did. */
  559.       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Stripping tail");
  560.       sp->data_length = sp->loop_end;
  561.     }
  562.     }
  563.  
  564.   close_file(fp);
  565.   return ip;
  566. }
  567.  
  568. static int fill_bank(int dr, int b)
  569. {
  570.   int i, errors=0;
  571.   ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
  572.   if (!bank)
  573.     {
  574.       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
  575.        "Huh. Tried to load instruments in non-existent %s %d",
  576.        (dr) ? "drumset" : "tone bank", b);
  577.       return 0;
  578.     }
  579.   for (i=0; i<128; i++)
  580.     {
  581.       if (bank->tone[i].instrument==MAGIC_LOAD_INSTRUMENT)
  582.     {
  583.       if (!(bank->tone[i].name))
  584.         {
  585.           ctl->cmsg(CMSG_WARNING, (b!=0) ? VERB_VERBOSE : VERB_NORMAL,
  586.            "No instrument mapped to %s %d, program %d%s",
  587.            (dr)? "drum set" : "tone bank", b, i, 
  588.            (b!=0) ? "" : " - this instrument will not be heard");
  589.           if (b!=0)
  590.         {
  591.           /* Mark the corresponding instrument in the default
  592.              bank / drumset for loading (if it isn't already) */
  593.           if (!dr)
  594.             {
  595.               if (!(standard_tonebank.tone[i].instrument))
  596.             standard_tonebank.tone[i].instrument=
  597.               MAGIC_LOAD_INSTRUMENT;
  598.             }
  599.           else
  600.             {
  601.               if (!(standard_drumset.tone[i].instrument))
  602.             standard_drumset.tone[i].instrument=
  603.               MAGIC_LOAD_INSTRUMENT;
  604.             }
  605.         }
  606.           bank->tone[i].instrument=0;
  607.           errors++;
  608.         }
  609.       else if (!(bank->tone[i].instrument=
  610.              load_instrument(bank->tone[i].name, 
  611.                      (dr) ? 1 : 0,
  612.                      bank->tone[i].pan,
  613.                      bank->tone[i].amp,
  614.                      (bank->tone[i].note!=-1) ? 
  615.                      bank->tone[i].note :
  616.                      ((dr) ? i : -1),
  617.                      (bank->tone[i].strip_loop!=-1) ?
  618.                      bank->tone[i].strip_loop :
  619.                      ((dr) ? 1 : -1),
  620.                      (bank->tone[i].strip_envelope != -1) ? 
  621.                      bank->tone[i].strip_envelope :
  622.                      ((dr) ? 1 : -1),
  623.                      bank->tone[i].strip_tail )))
  624.         {
  625.           ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
  626.            "Couldn't load instrument %s (%s %d, program %d)",
  627.            bank->tone[i].name,
  628.            (dr)? "drum set" : "tone bank", b, i);
  629.           errors++;
  630.         }
  631.     }
  632.     }
  633.   return errors;
  634. }
  635.  
  636. int load_missing_instruments(void)
  637. {
  638.   int i=128,errors=0;
  639.   while (i--)
  640.     {
  641.       if (tonebank[i])
  642.     errors+=fill_bank(0,i);
  643.       if (drumset[i])
  644.     errors+=fill_bank(1,i);
  645.     }
  646.   return errors;
  647. }
  648.  
  649. void free_instruments(void)
  650. {
  651.   int i=128;
  652.   while(i--)
  653.     {
  654.       if (tonebank[i])
  655.     free_bank(0,i);
  656.       if (drumset[i])
  657.     free_bank(1,i);
  658.     }
  659. }
  660.  
  661. int set_default_instrument(char *name)
  662. {
  663.   Instrument *ip;
  664.   if (!(ip=load_instrument(name, 0, -1, -1, -1, 0, 0, 0)))
  665.     return -1;
  666.   if (default_instrument)
  667.     free_instrument(default_instrument);
  668.   default_instrument=ip;
  669.   default_program=SPECIAL_PROGRAM;
  670.   return 0;
  671. }
  672.